const app = getApp();
var config = app.globalData.config;
const http = require('../../utils/http.js');

Page({
	data: {
		chooseid: 1,
		cid: 0,
		time: '',
		coursearrange_id:0,
		dataList: [],
		studentList: [{
			studentname: "蔡鸿睿",
			mobile: "",
			id: 66,
			status: 1
		}],
		is_showSubmit:'0'
	},
	choose(e) {
		this.setData({
			chooseid: e.currentTarget.dataset.id
		});
		this.getRollCall('', e.currentTarget.dataset.id);
	},
	onLoad(options) {
		this.toToday();
		var is_showSubmit = this.data.is_showSubmit;
		this.data.cid = options.cid;
		this.data.time = options.time;
		var coursearrange_id = options.coursearrange_id;
		
		var nowTime = (new Date(this.data.year+'-'+this.data.month+'-'+this.data.day)).getTime()/1000;
		var dtime = (new Date(this.data.time)).getTime()/1000;
	
		if(nowTime==dtime){//非当天的不显示确认提交
			is_showSubmit = '1';
		}
		
		this.setData({
			cid: options.cid,//课程id
			time: options.time,
			coursearrange_id:coursearrange_id,//课节id
			is_showSubmit:is_showSubmit
		});
		this.getRollCall();
	},
	toToday() {
		let year = new Date().getFullYear(),
			month = new Date().getMonth() + 1,
			day = new Date().getDate();
		if(month<=9){
			month = '0'+month;
		}
		if(day<=9){
			day = '0'+day;
		}
			
		this.setData({
			year,
			day,
			month
		})
	},
	//搜索
	searchFun(e) {
		var searchName = e.detail.value;
		this.getRollCall(searchName);
		// console.log(e);
		this.setData({
			chooseid: 1
		})
	},
	getRollCall(searchName = null, attendStatus = null) {
		var that = this;
		http.httpPost(config.rollCall, {
			curriculum_id: that.data.cid,
			coursearrange_id:that.data.coursearrange_id,
			dtimme: that.data.time,
			attendStatus: attendStatus,
			search_name: searchName
		}, function(e) {
			if (e.code == 1) {
				console.log(e);
				that.setData({
					'dataList': e.data,
					'studentList': e.data.studentList
				});
			} else {

			}

		});
	},
	//请假btn
	leaveBtn(e) {
		var student_id = e.currentTarget.dataset.student_id;
		var findex = e.currentTarget.dataset.findex;
		console.log(findex);
		var dataList = this.data.studentList;
		dataList[findex].status = 3;

		this.setData({
			'studentList': dataList
		});
	},
	//缺勤btn
	absenceBtn(e) {
		var student_id = e.currentTarget.dataset.student_id;
		var findex = e.currentTarget.dataset.findex;

		var dataList = this.data.studentList;
		dataList[findex].status = 2;

		this.setData({
			'studentList': dataList
		});
	},
	//正常btn
	normalBtn(e) {
		var student_id = e.currentTarget.dataset.student_id;
		var findex = e.currentTarget.dataset.findex;
		console.log(findex);
		var dataList = this.data.studentList;
		dataList[findex].status = 1;
	
		this.setData({
			'studentList': dataList
		});
	},
	submitBtn() { //确认提交按钮
		var dataJson = JSON.stringify(this.data.studentList)
		// console.log(dataJson);
		var that = this;
		http.httpPost(config.pushRollCall, {
			dataJson: dataJson
		}, function(e) {
			if (e.code == 1) {
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 1000,
          mask: true
        })
			} else {
        wx.showToast({
          title: '提交失败',
          icon: 'success',
          duration: 1000,
          mask: true
        })
			}
		});
	},
	onReady() {
		if (!app.login()) {
			return;
		}
	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})
